﻿using Abp.Domain.Repositories;
using AbpDemo.Entity;
using study.Tasks.Dto;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace study.Tasks
{
    public class TaskAppService : studyAppServiceBase, ITaskAppservice
    {
        private readonly IRepository<Task> _taskRepository;
        public TaskAppService(IRepository<Task> taskRepository)
        {
            _taskRepository = taskRepository;
        }
        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="input"></param>
        public void CreateTask(CreateTaskDto input)
        {
            //rgb(50, 255, 255);
            Random random = new Random();
            int r = random.Next(1, 255);
            int g = random.Next(1, 255);
            int b = random.Next(1, 255);

            var n_color = "rgb(" + r + "," + g + "," + b + ")";


            Task task = new Task() { Code = input.Code, Name = input.Name, Background_Color = n_color };
            _taskRepository.Insert(task);
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="input"></param>
        public void DelTask(TaskDto input)
        {
            Task task = _taskRepository.Get(input.Id);
            _taskRepository.Delete(task);
        }
        /// <summary>
        /// 通过id获取task name
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public Task GetTaskName(int id)
        {
            var taskAll = _taskRepository.Get(id);
            return taskAll;
        }
        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public List<Task> OutPutAllTask(TaskDto input)
        {
            var taskAll = _taskRepository.GetAll();
            if (input.Keyword != "")
            {
                taskAll = taskAll.Where(c => c.Code.Contains(input.Keyword) || c.Name.Contains(input.Keyword));
            }

            return taskAll.ToList();
        }
        /// <summary>
        /// 编辑
        /// </summary>
        /// <param name="input"></param>
        public void UpdateTask(TaskDto input)
        {
            Task task = _taskRepository.Get(input.Id);
            task.Code = input.Code;
            task.Name = input.Name;
            _taskRepository.Update(task);
        }

       
    }
}
